/**
* @author maze | guavestudios.com
* @license MIT License see LICENSE.TXT
* @version 0.1
*/
package com.guavestudios.fcon.plugins {
	import flash.events.KeyboardEvent;
	import flash.utils.ByteArray;
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.commands.UnlockConsoleCommand;
	import com.guavestudios.fcon.events.ConsoleEvent;
	import com.guavestudios.fcon.events.ConsoleExecuteEvent;

	import com.adobe.crypto.SHA256;
	
	public class LockManager implements IConsolePlugin{
		
		//{ STATIC
		
		//}
		
		//{ MEMBERS
		private var console:IControlableConsole;
		
		private var codeStart:Boolean=false;
		private var code:String;
		private var unlocked:Boolean=false;
		private var enteredCode:String="";
		//}
		
		//{ GETTER-SETTER
		
		//}
		public function LockManager() {
			
			
			init();	
		}
		private function init():void {
			
		}
		
		
		//{ PUBLIC FUNCTIONS
		public function unlock(c:String):void
		{
			if (SHA256.hash(c)==code) unlocked=true;
		}
		/**
		* Called when the plugin is finally loaded
		* @param	c
		*/
		public function initPlugin(c:IControlableConsole):void
		{
			console=c;
			
			code=c.getConfig("LockManager.password");
			if (code==null)
			{
				c.outputText("LockManager not correctly installed (no config): disabled");
				return;
			}
			c.getConfig("LockManager.password").setChildren(null);
			Console.trace("(LockManager.initPlugin) DELETED: "+c.getConfig("LockManager.password"));
			c.addEventListener(ConsoleEvent.CONSOLE_TOGGLE,console_toggle);
			
			//commands
			c.addCommand(new UnlockConsoleCommand());
		}
		/**
		* Request the unload for the plugin
		*/
		public function unloadPlugin():void
		{
			console.removeEventListener(ConsoleEvent.CONSOLE_TOGGLE,console_toggle);
		}
		/**
		* Current state of the plugin see @see com.guavestudios.fcon.plugins.PluginState
		*/
		public function get State():int
		{
			return 0;
		}
		/**
		* Plugin name
		*/
		public function get Name():String
		{
			return "LockManager";
		}
		/**
		* Plugin version
		*/
		public function get Version():String
		{
			return "0.0";
		}
		//}
		
		//{ OVERRIDEN METHODS
		
		//}
		
		//{ PRIVATE FUNCTIONS
		private function console_toggle(e:ConsoleExecuteEvent):void
		{
			if (!unlocked)
			{
				if (codeStart)
				{
					//trace("code end: "+enteredCode);
					codeStart=false;
					console.getStage().removeEventListener(KeyboardEvent.KEY_UP,key_up);
					if (SHA256.hash(enteredCode)==code)
					{
						unlocked=true;
						enteredCode="";
						return;
					}
					else
					{
						enteredCode="";
					}
					
				}
				else
				{
					codeStart=true;
					console.getStage().addEventListener(KeyboardEvent.KEY_UP,key_up);
				}
				
				
				e.Cancel=true;
			}
		}
		private function key_up(e:KeyboardEvent):void
		{
			if (e.charCode==0 ||e.keyCode==16 || console.getOpenKey().matchEvent(e)) return;
			
			enteredCode += String.fromCharCode(e.charCode);
			if (enteredCode.length>8)
			{
				codeStart = false;
				enteredCode="";
				console.getStage().removeEventListener(KeyboardEvent.KEY_UP,key_up);
			}
		}
		//}

	}
	
}
